package com.auto.learning.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.adapter.CommonAdapter;
import com.auto.learning.adapter.ViewHolder;
import com.auto.learning.bean.UserInfoBean;
import com.auto.learning.bean.UserVipInfo;
import com.auto.learning.event.EventBus;
import com.auto.learning.pay.PayUtils;
import com.auto.learning.service.MyUserInfo;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.tools.Utils;
import com.auto.learning.view.PupPayDialog;
import com.auto.learning.view.PupPayDialog.OnPayItemClick;
import com.squareup.otto.Subscribe;
import com.squareup.otto.ThreadEnforcer;

public class MyMoneyActivity extends BaseActivity {
	
	private CommonAdapter<String> adapter;
	private List<String> list = new ArrayList<String>();
	private int defaultCheckMoney = 0;
	private TextView tv_mymoney;
	private GridView gr_money;
	private Button btn_submit;

	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				initDate();
				break;
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_money);
		setTitle("账户余额");
		initgetData();
		initView();
		initDate();
		regeditEvent();
		TemporaryVariable.activities.push(this);
	}
	public void initgetData(){
		Intent intent=getIntent();
		if(intent!=null){
			TemporaryVariable.bookmoney.setType(intent.getIntExtra("type", 0));
			TemporaryVariable.bookmoney.setBookId(intent.getIntExtra("bookId", -1));
			TemporaryVariable.bookmoney.setListid(intent.getIntExtra("listId", -1));
			TemporaryVariable.bookmoney.setBookName(intent.getStringExtra("bookName"));
			TemporaryVariable.bookmoney.setBookMoney(intent.getDoubleExtra("bookMoney", 0.0));
			TemporaryVariable.bookmoney.setName(intent.getStringExtra("listName"));
		}else{
			TemporaryVariable.bookmoney.setType(0);
		}
	}
	@Override
	protected void onDestroy() {
		TemporaryVariable.activities.removeElement(this);
		unRegeditEvent();
		super.onDestroy();
	}
	
	@Subscribe
	public void onWXRechargeSuccessEvent(Integer payMoney){
		handler.obtainMessage(1, payMoney).sendToTarget();
	}
	
	
	private void regeditEvent(){
		EventBus.getEventBus("weixin_recharge_success", ThreadEnforcer.MAIN).register(this);
		
	}
	
	private void unRegeditEvent(){
		EventBus.getEventBus("weixin_recharge_success", ThreadEnforcer.MAIN).unregister(this);
	}

	protected void initView() {
		// TODO Auto-generated method stub
		
		gr_money = (GridView) findViewById(R.id.gr_money);
		tv_mymoney = (TextView) findViewById(R.id.tv_mymoney);
		adapter = new CommonAdapter<String>(this, list, R.layout.item_mymoney) {

			@SuppressLint("NewApi")
			@Override
			public void convert(ViewHolder viewHolder, String item) {
				// TODO Auto-generated method stub
				RelativeLayout rl_bg = viewHolder.getView(R.id.rl_bg);
				// SDK版本判断
				if (viewHolder.getPosition() == defaultCheckMoney) {
					if (Utils.getAndroidSDKVersion() > 15) {
						rl_bg.setBackground(getResources().getDrawable(R.drawable.item_zb_money_bg));
					} else {
						rl_bg.setBackgroundDrawable(getResources().getDrawable(R.drawable.item_zb_money_bg));
					}

				} else {
					if (Utils.getAndroidSDKVersion() > 15) {
						rl_bg.setBackground(getResources().getDrawable(R.drawable.item_zb_money_bg1));
					} else {
						rl_bg.setBackgroundDrawable(getResources().getDrawable(R.drawable.item_zb_money_bg1));
					}
				}
				viewHolder.setText(R.id.tv_text, item);
			}
		};
		gr_money.setAdapter(adapter);
		gr_money.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				defaultCheckMoney = position;
				adapter.notifyDataSetChanged();
			}
		});
		btn_submit = (Button) findViewById(R.id.btn_submit);
		btn_submit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				PupPayDialog pup_Pay_Dialog = new PupPayDialog(MyMoneyActivity.this, Integer.parseInt(list.get(defaultCheckMoney)));
				pup_Pay_Dialog.setOnPayItemClick(new OnPayItemClick() {
					
					@Override
					public void onWechatPayClick(int money) {
						new PayUtils(MyMoneyActivity.this, 2, money * 100).pay();
					}
					
					@Override
					public void onAliPayClick(int money) {
						new PayUtils(MyMoneyActivity.this, 1, money * 100).pay();
					}
				});
				pup_Pay_Dialog.show(tv_mymoney);
			}
		});
	}
	
	/**
	 * 充值
	 */
	public void rechargeSuccess(int money){
		UserInfoBean userInfoBean = MyUserInfo.getInstance().getUserInfoBean();
		if (userInfoBean.getVipInfo() != null) {
			UserVipInfo userVipInfo = userInfoBean.getVipInfo();
			userVipInfo.setBalanceFen(userVipInfo.getBalanceFen() + money);
			userVipInfo.setBalanceYuan(CommonMethod.calculationMoney(userVipInfo.getBalanceFen()));
			MyUserInfo.getInstance().getUserInfoBean().setVipInfo(userVipInfo);
		}
		initDate();
	}

	protected void initDate() {
		UserInfoBean userInfoBean = MyUserInfo.getInstance().getUserInfoBean();
		if (userInfoBean != null && userInfoBean.getVipInfo() != null) {
			tv_mymoney.setText(userInfoBean.getVipInfo().getBalanceYuan()+"");
			if (list != null) {
				list.clear();
			}
			String moneys = userInfoBean.getConfig().getRechargeMoney();
			String[] str = moneys.split("/");
			for (int i = 0; i < str.length; i++) {
				list.add(str[i]);
			}
			adapter.notifyDataSetChanged();
		}
	}

}
